<!-- 标签列表 -->
<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="110px"
    >
      <el-form-item label="关键词" prop="keyword">
        <el-input
          v-model="queryParams.keyword"
          placeholder="请输入标签名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标签所属分类" prop="groupValue">
        <el-select
          v-model="queryParams.groupValue"
          placeholder="请选择标签所属分类"
          style="width: 100%"
          clearable
          @change="handleQuery"
        >
          <el-option
            v-for="dict in dict.type.disease_tag_group"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb-8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleOpen()"
          >新增</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="标签名称" align="center" prop="tagName" />
      <el-table-column label="标签图标" align="center" prop="tagName">
        <template slot-scope="{ row }">
          <image-preview
            v-if="row.tagLogo"
            :src="row.tagLogo"
            :width="50"
            :height="50"
          />
        </template>
      </el-table-column>
      <el-table-column label="标签疾病数据" align="center" prop="diseaseNum" />
      <el-table-column label="标签状态" align="center">
        <template slot-scope="{ row }">
          <el-switch
            v-model="row.status"
            :active-value="0"
            active-text="启用"
            :inactive-value="1"
            inactive-text="禁用"
            @change="changeStatus(row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="标签所属分类" align="center" prop="groupName">
        <template slot-scope="{ row }">
          {{ row.groupName || "未分类" }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="120px"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleOpen(scope.row.id)"
            >编辑</el-button
          >
          <el-button size="mini" type="text" @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <EditTag ref="EditTag" @refresh="getList" />
  </div>
</template>

<script>
import { getTagList, deleteTag, updateTag } from "@/api/saas/tcm.js";
import EditTag from "./editTag";
export default {
  name: "TagList",
  dicts: [
    "disease_tag_group"
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        keyword: "",
        groupValue: undefined
      },
      // 总条数
      total: 0,
      dataList: [],
    };
  },
  components: {
    EditTag,
  },
  created() {
    this.getList();
  },
  activated() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      getTagList(this.queryParams)
        .then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      Object.assign(this.$data.queryParams, this.$options.data().queryParams);
      this.getList();
    },
    handleOpen(id) {
      this.$refs.EditTag.openModal(id);
    },
    /** 删除按钮操作 */
    handleDelete(id) {
      this.$modal
        .confirm("是否确认删除该标签吗？")
        .then(function () {
          return deleteTag(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    // 修改状态
    changeStatus(row) {
      updateTag(row).then(res => {
        this.$message.success('状态修改成功');
      }).catch((err) => {
        row.status = row.status ? 0 : 1; // 修改失败,复位
      })
    },
  },
};
</script>
